//===================================================
//File name: MenuTool.java			Version: 1.0
//Created: Tue 12 Feb, 2008
//Creator: Shabal Goel
//
//Purpose: Provide an implementation of the user interface for the Menu Tool
//
//Side effects: Changes the properties of Menu objects
//
//===================================================

import java.util.ArrayList;
import java.util.Scanner;

//===============================
//CONSTRUCTOR
//Purpose: Create and display a Menu Item with default options, title, items, and prompt
//===============================
public class MenuTool {
	private Menu aMenu;
	private int depth = 0;
	private char output;
	
	public MenuTool () {
		
		ArrayList<Item> items = new ArrayList<Item>();
		ArrayList<Item>options = new ArrayList<Item>();
		
		items.add (new Item ('a', "Item 1"));
		items.add (new Item ('b', "Item 2"));
		items.add (new Item ('c', "Item 3"));
		
		options.add (new Item ('1', "Set Title"));
		options.add (new Item ('2', "Set Key"));
		options.add (new Item ('3', "Set Item Description"));
		options.add (new Item ('4', "Set Prompt"));
		options.add (new Item ('5', "Add Item"));
		options.add (new Item ('6', "Remove Item"));
		options.add (new Item ('7', "Create Sub-Menu"));
		options.add (new Item ('8', "Select Item"));
		
		aMenu = new Menu (items, "TITLE", "Prompt", options, depth);
		aMenu.display();		
	}
	//=============================
	//Function name: start
	//
	//Purpose: Read user input from the input stream and execute corresponding function
	//
	//Input: the Menu object on which the below functions can be executed on
	//Output: the key ultimately selected by the user
	//=============================
	public char start(Menu pMenu) {
		try {	
			char input = (char)System.in.read();
			//Loop through until the user selects an item key to return
			while (input != '8') {
				switch (input) {
					case '1':
						setTitle(pMenu);
						break;
					case '2':
						setKey(pMenu);
						break;
					case '3':
						setItemDesc(pMenu);
						break;
					case '4':
						setPrompt(pMenu);
						break;
					case '5':
						addItem(pMenu);
						break;
					case '6':
						remItem(pMenu);
						break;
					case '7':
						output = subMenu(pMenu);
						return output;
					case '8':
						break;
					default:
						System.out.print("Invalid Selection");
						System.exit(0);
				}
							
				if (input != '7')
					pMenu.display();
				if (input != '8') 
					input = (char)System.in.read();
		
			}
		}
		catch(Exception exe) { 
			exe.printStackTrace(); 
		}
		
		output = selectItem();
		return output;
	}
	
	private void setTitle(Menu pMenu) {
		System.out.print("New Title: ");
		
		Scanner scan = new Scanner(System.in);
		String nTitle = scan.next();
		
		pMenu.setTitle(nTitle);
	}
	
	private void setKey(Menu pMenu) {	
		System.out.print("Old Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char oKey = tString.charAt(0);
		
		System.out.print("New Key: ");
		String tString2 = scan.next();
		char nKey = tString2.charAt(0);
		
		pMenu.setKey(oKey, nKey);
	}
	
	private void setItemDesc(Menu pMenu) {
		System.out.print("Enter Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char key = tString.charAt(0);
		Item temp = aMenu.getItem(key);
		
		System.out.print("New Description: ");
		String nDesc = scan.next();
		
		pMenu.setDesc(key, nDesc);
	}
	
	private void setPrompt(Menu pMenu) {
		System.out.print("New Prompt: ");
		
		Scanner scan = new Scanner(System.in);
		String nPrompt = scan.next();
		
		pMenu.setPrompt(nPrompt);
	}
	
	private void addItem(Menu pMenu) {
		System.out.print("Enter Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char key = tString.charAt(0);
		
		System.out.print("Enter Description: ");
		String desc = scan.next();
		
		Item nItem = new Item (key, desc);
		pMenu.addItem(nItem);
	}
	
	private void remItem(Menu pMenu) {	
		System.out.print("Enter Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char key = tString.charAt(0);
		
		pMenu.removeItem(pMenu.getItem(key));
	}
	
	//==============================
	//Function name: subMenu
	//
	//Purpose: create and display a new menu with increased depth under a user-selected item
	//
	//Input: the Menu under which the submenu will be created
	//Output: the key ultimately selected by the user in the new submenu
	//
	//Notes: Upon displaying the new submenu, the method recursively calls the method start(newMenu) to receive further user input
	//==============================
	private char subMenu(Menu pMenu) {
		ArrayList<Item> newItems = new ArrayList<Item>();
		ArrayList<Item> newOptions = new ArrayList<Item>();
		
		newItems.add (new Item ('a', "Item 1"));
		newItems.add (new Item ('b', "Item 2"));
		newItems.add (new Item ('c', "Item 3"));
		
		newOptions.add (new Item ('1', "Set Title"));
		newOptions.add (new Item ('2', "Set Key"));
		newOptions.add (new Item ('3', "Set Item Description"));
		newOptions.add (new Item ('4', "Set Prompt"));
		newOptions.add (new Item ('5', "Add Item"));
		newOptions.add (new Item ('6', "Remove Item"));
		newOptions.add (new Item ('7', "Create Sub-Menu"));
		newOptions.add (new Item ('8', "Select Item"));	
				
		System.out.print("Enter Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char key = tString.charAt(0);
		
		depth++;
		System.out.print(pMenu.getItem(key));
		Menu newMenu = new Menu (newItems, "TITLE", "Prompt", newOptions, depth);
		newMenu.display();
		
		return start(newMenu);
	}
	
	private char selectItem() {
		System.out.print("Enter Key: ");
		Scanner scan = new Scanner(System.in);
		String tString = scan.next();
		char key = tString.charAt(0);
		
		return key;
	}
	
	public Menu getStartMenu() {
		return aMenu;
	}
}